/**
 *
 * User: Zhikang.Peng
 * Date: 2018/04/13 13:44
 */
package control

import (
	"gitee.com/thghh/jvmgo/instructions/base"
	"gitee.com/thghh/jvmgo/rtda"
)

/**
 * 用于switch条件跳转，case值不连续（可变长度指令）
 * defaultOffset 默认跳转所需的字节偏移量
 */
type LOOKUP_SWITCH struct {
	defaultOffset int32
	npairs        int32
	matchOffsets  []int32
}

func (self *LOOKUP_SWITCH) FetchOperands(reader *base.BytecodeReader) {
	reader.SkipPadding()
	self.defaultOffset = reader.ReadInt32()
	self.npairs = reader.ReadInt32()
	self.matchOffsets = reader.ReadInt32s(self.npairs * 2)
}

/**
先从操作数栈中弹出一个int变量，然后用它查找
matchOffsets，看是否能找到匹配的key。如果能，则按照value给出的
偏移量跳转，否则按照defaultOffset跳转
*/
func (self *LOOKUP_SWITCH) Execute(frame *rtda.Frame) {
	key := frame.OperandStack().PopInt()
	for i := int32(0); i < self.npairs*2; i += 2 {
		if self.matchOffsets[i] == key {
			offset := self.matchOffsets[i+1]
			base.Branch(frame, int(offset))
			return
		}
	}
	base.Branch(frame, int(self.defaultOffset))
}
